package com.itheima.controller;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptServiceImpl deptService;

    @Autowired
    private EmpMapper empMapper;

    /*
     * 查询所有的部门数据
     * @return   存储Dept对象的集合
     * */
    @GetMapping
    public Result select() {
        log.info("查询所有部门数据");
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    /*
     * 根据id删除部门信息
     * @param id   部门id
     * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        //日志记录
        log.info("根据id删除部门");
        deptService.delect(id);
        int i = 1/0;
        empMapper.deleteByDeptId(id);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Dept dept) {
        //记录日志
        log.info("新增部门：{}", dept);
        deptService.insert(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result select2(@PathVariable Integer id) {
        Dept dept = deptService.select2(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }
}
